import pygame
import sys

# 初始化Pygame
pygame.init()

# 设置窗口尺寸
width, height = 640, 480
size = (width, height)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame示例")

# 设置小球的初始位置和速度
ball_radius = 20
ball_pos = [width // 2, height // 2]
ball_speed = [2, 2]

# 游戏主循环
while True:
    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # 更新小球位置
    ball_pos[0] += ball_speed[0]
    ball_pos[1] += ball_speed[1]

    # 检测小球是否碰到窗口边界
    if ball_pos[0] < ball_radius or ball_pos[0] > width - ball_radius:
        ball_speed[0] = -ball_speed[0]
    if ball_pos[1] < ball_radius or ball_pos[1] > height - ball_radius:
        ball_speed[1] = -ball_speed[1]

    # 填充窗口背景色
    screen.fill((0, 0, 0))

    # 在窗口中绘制小球
    pygame.draw.circle(screen, (255, 255, 255), ball_pos, ball_radius)

    # 更新窗口显示
    pygame.display.flip()